﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfPaintIntersect
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="MainWindow"/> class.
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
        }

        #endregion

        #region Canvas methods

        Line line;

        private void PolyCanvas_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var point = e.GetPosition(PolyCanvas);

            if (line == null)
            {
                line = new Line();
                line.Stroke = Brushes.Blue;
                line.StrokeThickness = 2.0;

                line.X2 = point.X;
                line.Y2 = point.Y;

                PolyCanvas.Children.Add(line);
            }

            line.X1 = point.X;
            line.Y1 = point.Y;
        }

        private void PolyCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            var point = e.GetPosition(PolyCanvas);
            if (line != null)
            {
                line.X2 = point.X;
                line.Y2 = point.Y;
            }
        }

        private void PolyCanvas_MouseUp(object sender, MouseButtonEventArgs e)
        {
            line = null;
        }

        #endregion

        #region Buttons

        private void ClearButton_Click(object sender, RoutedEventArgs e)
        {
            PolyCanvas.Children.Clear();
            line = null;
        }

        private void EarButton_Click(object sender, RoutedEventArgs e)
        {
            IList<Line> lines = new List<Line>();
            foreach (var child in PolyCanvas.Children)
            {
                if (child is Line)
                {
                    lines.Add((Line)child);
                }
            }

            for (int i = 0; i < lines.Count; i++)
            {
                for (int j = 0; j < lines.Count; j++)
                {
                    if (i <= j)
                        continue;

                    Line line1 = lines[i];
                    Line line2 = lines[j];
                    Point intersect;

                    if (ClippingEar.LineIntersect(
                        new Point(line1.X1, line1.Y1), new Point(line1.X2, line1.Y2),
                        new Point(line2.X1, line2.Y1), new Point(line2.X2, line2.Y2),
                        out intersect
                        ))
                    {
                        Ellipse circle = new Ellipse();
                        circle.Width = 10;
                        circle.Height = 10;
                        circle.Stroke = Brushes.Purple;
                        circle.Fill = Brushes.Purple;

                        Canvas.SetZIndex(circle, 1);
                        Canvas.SetLeft(circle, intersect.X - 0.5 * circle.Width);
                        Canvas.SetTop(circle, intersect.Y - 0.5 * circle.Height);

                        PolyCanvas.Children.Add(circle);

                        line1.Stroke = Brushes.Red;
                        line2.Stroke = Brushes.Red;
                    }
                }
            }
        }

        #endregion
    }
}
